/*
 * BookstoreFacadeStats.java
 *
 * Created on 12 août 2011, 09:45
 */
package com.oalam.bookstore.facade;

import javax.management.*;

/**
 * Class BookstoreFacadeStats
 *
 * @author Administrateur
 */
public class BookstoreFacadeStats implements BookstoreFacadeStatsMBean, NotificationEmitter {

    private NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
    private long seqNumber;
    /**
     * Attribute : NbAccountsCreated
     */
    private int nbAccountsCreated;

    public BookstoreFacadeStats() {
    }

    /**
     * Get Counts the accounts
     */
    public int getNbAccountsCreated() {
        return nbAccountsCreated;
    }

    /**
     * Set Counts the accounts
     */
    public void setNbAccountsCreated(int value) {
        nbAccountsCreated = value;
    }

    public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws IllegalArgumentException {
        broadcaster.addNotificationListener(listener, filter, handback);
    }

    public MBeanNotificationInfo[] getNotificationInfo() {
        return new MBeanNotificationInfo[]{new MBeanNotificationInfo(new String[]{AttributeChangeNotification.ATTRIBUTE_CHANGE}, javax.management.AttributeChangeNotification.class.getName(), "New account created")};
    }

    public void removeNotificationListener(NotificationListener listener) throws ListenerNotFoundException {
        broadcaster.removeNotificationListener(listener);
    }

    public void removeNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws ListenerNotFoundException {
        broadcaster.removeNotificationListener(listener, filter, handback);
    }

    public synchronized long getNextSeqNumber() {
        return seqNumber++;
    }

    /**
     * increment nb account
     */
    public void addAccount() {
        nbAccountsCreated++;

        if (nbAccountsCreated > 5) {
            //Create a JMX Notification
            Notification notification = new Notification(AttributeChangeNotification.ATTRIBUTE_CHANGE,
                    this,
                    getNextSeqNumber(),
                    "account created ");

            // Send a JMX notification.
            broadcaster.sendNotification(notification);
        }
    }
}
